package com.liuheng.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuheng.spzx.model.dto.h5.ProductSkuDto;
import com.liuheng.spzx.model.dto.product.SkuSaleDto;
import com.liuheng.spzx.model.entity.base.BaseEntity;
import com.liuheng.spzx.model.entity.product.ProductSku;
import com.liuheng.spzx.model.vo.common.Result;
import com.liuheng.spzx.model.vo.common.ResultCodeEnum;
import com.liuheng.spzx.model.vo.h5.ProductItemVo;
import com.liuheng.spzx.product.service.ProductSkuService;
import com.liuheng.spzx.product.utils.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/product/")
public class ProductController {
    @Autowired
    private ProductSkuService productSkuService;
    @GetMapping("/{page}/{limit}")
    public Result findByPage(@PathVariable Integer page, @PathVariable Integer limit, ProductSkuDto productSkuDto) {
        Page<ProductSku> productSkuPage = new Page<>(page,limit);
        Page<ProductSku> page1=productSkuService.findPage(productSkuPage,productSkuDto);
        PageInfo<ProductSku> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(page1,pageInfo);
        pageInfo.setList(page1.getRecords());
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/item/{skuId}")
    public Result findBySkuId(@PathVariable Long skuId) {
        ProductItemVo productItemVo=productSkuService.item(skuId);
        return Result.build(productItemVo, ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/getBySkuId/{skuId}")
    public ProductSku getBySkuId( @PathVariable Long skuId) {
        ProductSku productSku =productSkuService.getById(skuId);
        return productSku;
    }
    @PostMapping("updateSkuSaleNum")
    public Boolean updateSkuSaleNum(@RequestBody List<SkuSaleDto> skuSaleDtoList) {
        for (SkuSaleDto skuSaleDto : skuSaleDtoList) {
            Integer num = skuSaleDto.getNum();
            ProductSku productSku = productSkuService.getById(skuSaleDto.getSkuId());
            productSku.setSaleNum(productSku.getSaleNum()+num);
            productSku.setStockNum(productSku.getStockNum()-num);
            productSkuService.updateById(productSku);
        }
        return true;
    }
}
